package com.gonsin.ta.logic.parse.condition;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gonsin.ta.logic.parse.condition.context.ChainContextImpl;
import com.gonsin.ta.logic.parse.serialize.FutureConditionSerial;
import com.gonsin.ta.logic.parse.serialize.SerializeContext;
import com.gonsin.ta.logic.parse.serialize.TaDeserializable;
import com.google.gson.annotations.Expose;

import java.io.Serializable;

public class FutureCondition implements Executable, Serializable {

    /**
     * 指向方法的名字
     */
    private String conditionName;

    /**
     * 指向condition的主键
     */
    private String conditionId;

    public FutureCondition(){
    }

    public void setFutureExecutable(Condition futureExecutable) {
        if(this.futureExecutable != null){
            return;
        }
        this.conditionId = futureExecutable.getConditionId();
        this.futureExecutable = futureExecutable;
    }

    @JsonIgnore
    @Expose(serialize = false, deserialize = false)
    private transient Condition futureExecutable;

    @Override
    public void execute(ChainContextImpl context) throws Exception {
        futureExecutable.execute(context);
    }

    @Override
    public TaDeserializable toSerializable(SerializeContext serializeContext) {

        if(serializeContext.hasHandle(this)){
            FutureConditionSerial serial = new FutureConditionSerial();
            serial.setConditionId(futureExecutable.getConditionId());
            return serial;
        }
        serializeContext.handle(this);

        FutureConditionSerial serial = new FutureConditionSerial();
        serial.setConditionId(futureExecutable.getConditionId());
        serializeContext.putFutureConditionSerial(this.conditionId, futureExecutable.toSerializable(serializeContext));
        return serial;
    }

    public String getConditionName() {
        return conditionName;
    }

    public Condition getFutureExecutable() {
        return futureExecutable;
    }

    public void setConditionName(String conditionName) {
        this.conditionName = conditionName;
    }

    public String getConditionId() {
        return conditionId;
    }

    public void setConditionId(String conditionId) {
        this.conditionId = conditionId;
    }
}
